package com.tarena.dang.actions.order;

import com.tarena.dang.actions.BaseAction;
import com.tarena.dang.pojos.Address;
import com.tarena.dang.pojos.User;
import com.tarena.dang.services.AddressService;
import com.tarena.dang.services.CartFactory;
import com.tarena.dang.services.CartService;
import com.tarena.dang.services.OrderService;
import com.tarena.dang.services.impl.AddressServiceImpl;
import com.tarena.dang.services.impl.OrderServiceImpl;
import com.tarena.dang.utils.Constant;

public class FinishOrderAction extends BaseAction {
	private Address address;
	private int orderId;
	private double count;

	public String execute() {
		User user = (User) session.get(Constant.USER);
		if (user != null) {
			AddressService addressService = new AddressServiceImpl();
			OrderService orderService = new OrderServiceImpl();
			address.setUserId(user.getId());
			try {
				addressService.processingAddress(address);
				CartService cart = CartFactory.getCart(session);
				orderId = orderService.addOrder(address, cart);
				if (orderId != -1) {
					count = cart.count();
					session.remove(Constant.CART);

					return "success";
				} else {
					return "fail";
				}

			} catch (Exception e) {
				e.printStackTrace();
				return "fail";
			}
		} else {
			return "login";
		}
	}

	public int getOrderId() {
		return orderId;
	}

	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}

	public double getCount() {
		return count;
	}

	public void setCount(double count) {
		this.count = count;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

}
